package com.wffz.controller;

import com.wffz.comm.api.ResponseResult;
import com.wffz.comm.utils.CodeUtils;
import com.wffz.comm.utils.DateUtils;
import com.wffz.dto.UserDTO;
import com.wffz.po.ClientUser;
import com.wffz.po.IncomeLog;
import com.wffz.po.ShareRecord;
import com.wffz.po.WffzConfig;
import com.wffz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jodd.cli.Cli;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author sunlei
 * @className ClientUserController
 * @Description
 * @date 2022-12-28 9:39
 **/
@RestController
@Api(tags = "用户管理")
@Slf4j
public class ClientUserController {

    @Resource
    private UserService userService;
    @Resource
    private ShareRecordService service;

    @Resource
    private WffzConfigService configService;


    @Resource
    private IncomeLogService incomeLogService;
    @Resource
    private MobileVerificationCodeService mobileVerificationCodeService;

    /**
     * @Description: [用户密码验证]
     * @Title: validateUser
     * @Author: SUNLEI
     * @Date: 2023-04-10
     * @Param: userDTO
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @PostMapping("/validateUser")
    public ResponseResult validateUser(@ApiParam(value = "验证", required = true) @RequestBody UserDTO userDTO) {
        boolean flag = userService.validateUser(userDTO.getLoginName(), userDTO.getPassword());
        return ResponseResult.success(flag);
    }


    /**
     * @Description: [用户注册]
     * @Title: registerUser
     * @Author: SUNLEI
     * @Date: 2023-03-13
     * @Param: userDTO
     * @Return: com.wffz.comm.api.ResponseResult
     * @Throws:
     */
    @ApiOperation("用户注册")
    @PostMapping("/registerUser")
    public ResponseResult registerUser(@ApiParam(value = "注册用户", required = true) @RequestBody UserDTO userDTO) {
        boolean flag = mobileVerificationCodeService.isEfficacious(userDTO.getLoginName(), userDTO.getMobileCode());
        boolean isExistUser = userService.isExistClientUser(userDTO.getLoginName());
        // 注册金额
        String key = "register_amount";
        WffzConfig config = configService.getConfigByKey(key);
        // 注册金额
        String key1 = "share_amount";
        WffzConfig wffzConfig = configService.getConfigByKey(key1);
        if (!flag) {
            return ResponseResult.error("verification is false");
        }

        if (isExistUser) {
            return ResponseResult.error("The mobile phone number already exists ");
        }

        String rcode = userDTO.getRcode() == null ? "" : userDTO.getRcode();

        float registerBalance = Float.parseFloat(config.getConfigValue());

        ClientUser clientUser = new ClientUser();
        clientUser.setUserName(userDTO.getLoginName());
        clientUser.setLoginName(userDTO.getLoginName());
        clientUser.setMobileNumber(userDTO.getLoginName());
        clientUser.setLoginPassword(userDTO.getPassword());
        clientUser.setCreateTime(new Date());
        clientUser.setRcode(rcode);
        clientUser.setInvitationCode(CodeUtils.getInvitationCode(8));
        clientUser.setVipLevel(0);
        clientUser.setBalance(0.0f);
        clientUser.setAwayBalance(registerBalance);
        log.info(clientUser.getMobileNumber());
        boolean f = userService.saveClientUser(clientUser);
        // 注册赠送金额写入收入支出表
        IncomeLog in = new IncomeLog();
        in.setUserId(clientUser.getId());
        in.setLoginName(clientUser.getLoginName());
        in.setAmount(registerBalance);
        in.setCreateTime(new Date());
        in.setIncomeType("9");
        incomeLogService.saveInComeLog(in);

        // 根据rcode获取用户  给用户新增3000卢比
        if (!rcode.equals("")) {
            ClientUser u = userService.findUserByinvitationCode(rcode);
            // 如果是首次分享成功
            int count = service.getMyShareCount(u.getId());
            float amount = Float.parseFloat(wffzConfig.getConfigValue());
            if (count <= 0) {
                u.setAwayBalance(u.getAwayBalance() + amount + 10f);
            } else {
                u.setAwayBalance(u.getAwayBalance() + amount);
            }

            userService.updateClientUser(u);
            //分享
            IncomeLog incomeLog = new IncomeLog();
            incomeLog.setUserId(u.getId());
            incomeLog.setLoginName(u.getLoginName());
            incomeLog.setAmount(amount);
            incomeLog.setCreateTime(new Date());
            incomeLog.setIncomeType("4");
            incomeLogService.saveInComeLog(incomeLog);

            // 新增一条分享记录
            ShareRecord record = new ShareRecord();
            record.setInvitationCode(rcode);
            record.setLoginName(u.getLoginName());
            record.setUserId(u.getId());
            record.setInvitationLoginName(userDTO.getLoginName());
            record.setInvitationUserId(clientUser.getId());
            record.setCreateTime(DateUtils.getNowDate());
            record.setCreateDay(DateUtils.getNowDay());
            service.saveShareRecord(record);

            //如果是首次分享成功,赠送金额
            if (count <= 0) {
                IncomeLog in1 = new IncomeLog();
                in1.setUserId(u.getId());
                in1.setLoginName(u.getLoginName());
                in1.setAmount(10f);
                in1.setCreateTime(new Date());
                in1.setIncomeType("7"); // 首次分享任务
                incomeLogService.saveInComeLog(in1);
            }
        }
        return ResponseResult.success(f);
    }

}
